package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.ui.comp.Navigator;
import java.awt.BorderLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Quick view for the task.
 *
 * @author dimitri
 */
public class TaskQuickView extends JPanel {
    private static final long serialVersionUID = 2132392737953800744L;
    private TaskIndexTable table;
    private Navigator navigator;
    private StandardFolderList standardList;
    private Object lastObject;

    public TaskQuickView() {
        initComponents();
        standardList.setSelectedIndex(0);
        standardList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                Object selection = standardList.getSelectedValue();
                if (selection != null && !selection.equals(lastObject)) {
                    lastObject = selection;
                    reset();
                    table.refresh();
                }
            }
        });
        reset();
    }

    private void initComponents() {
        setLayout(new BorderLayout(5, 5));
        // add center component
        add(getCenterComponent(), BorderLayout.CENTER);
        // add navigator
        navigator = new Navigator();
        navigator.setTable(table);
        navigator.reset();
        add(navigator, BorderLayout.SOUTH);
    }

    private JComponent getCenterComponent() {
        // table
        table = new TaskIndexTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JPanel right = new JPanel(new BorderLayout());
        right.add(new JScrollPane(table), BorderLayout.CENTER);

        // standard list
        standardList = new StandardFolderList();
        JScrollPane scroll = new JScrollPane(standardList);
        scroll.setOpaque(false);
        JPanel left = new JPanel(new BorderLayout());
        left.add(scroll, BorderLayout.CENTER);

        // center component
        JSplitPane center = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
        center.setDividerLocation(150);
        return center;
    }

    public TaskIndexTable getTable() {
        return table;
    }

    private void reset() {
        StandardFolderList.FolderEntry entry = (StandardFolderList.FolderEntry) standardList.getSelectedValue();
        table.setConditions(entry.getConditions());
        table.setPage(0);
    }

    public void openTask() {
        table.openTask();
    }

}
